package 力扣日常刷题.木23一月.第03天303;

import 力扣日常刷题.ListNode;

/**
 * @author 帅小伙
 * @date 2022/3/3
 * @description
 */
public class Demo31合并K个上升链表 {


    // 拆分成多个插入
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode ans = null;
        for (int i = 0; i < lists.length; i++) {
            ans = mergeTwoLists(ans,lists[i]);
        }
        return ans;
    }

    public ListNode mergeTwoLists(ListNode a,ListNode b) {
        if (a == null || b == null) {
            return a != null ? a : b;
        }
        else if(a.val > b.val) {
            b.next = mergeTwoLists(a,b.next);
            return b;
        }else{
            a.next = mergeTwoLists(a.next,b);
            return a;
        }
    }

}
